/*
 * Project Name spring-boot-tools
 * File Name TestController
 * Package Name com.huxiaosu.tools.cmpp.controller
 * Create Time 2024/5/19
 * Create by name：liujie
 */
package com.huxiaosu.tools.cmpp.controller;

import com.huxiaosu.tools.cmpp.cache.CacheService;
import com.huxiaosu.tools.cmpp.config.CmppClientConfig;
import com.huxiaosu.tools.cmpp.enums.CommandId;
import com.huxiaosu.tools.cmpp.lib.SocketClient;
import com.huxiaosu.tools.cmpp.model.action.ConnectMsg;
import com.huxiaosu.tools.cmpp.utils.Utils;
import lombok.AllArgsConstructor;
import org.apache.mina.core.future.WriteFuture;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.huxiaosu.tools.cmpp.utils.Constants.CMPP_VERSION;

/**
 * Description
 *
 * @author liujie
 * @date 2024/5/19 11:07
 */
@AllArgsConstructor
@RestController
public class TestController {
    private final CacheService cacheService;
    private final CmppClientConfig cmppClientConfig;
    @GetMapping("testConnect")
    public String testConnect(){

        ConnectMsg connect = new ConnectMsg();
        //消息总长度，级总字节数:4+4+4(消息头)+6+16+1+4(消息主体)
        connect.setTotalLength(CommandId.CMPP_CONNECT.getMsgLength());
        //标识创建连接
        connect.setCommandId(CommandId.CMPP_CONNECT.getCommand());
        //序列，由我们指定
        connect.setSequenceId(Utils.getSequence());
        //我们的企业代码
        connect.setSourceAddr(cmppClientConfig.getSpId());
        //时间戳(MMDDHHMMSS)
        connect.setTimestamp(Integer.parseInt(Utils.getTimestamp()));
        byte[] authenticatorSource = Utils.getAuthenticatorSource(cmppClientConfig.getSpId(), cmppClientConfig.getSharedSecret(), connect.getTimestamp()+"");
        //md5(企业代码+密匙+时间戳)
        connect.setAuthenticatorSource(authenticatorSource);
        //版本号 高4bit为3，低4位为0
        connect.setVersion(CMPP_VERSION);
        IoSession session = (IoSession) cacheService.get(cmppClientConfig.getSpId());
        WriteFuture write = session.write(connect.toBytes());
        return write.isDone()+"";

    }

    @GetMapping("testSubmit")
    public String testSubmit(String msg){

        ConnectMsg connect = new ConnectMsg();
        //消息总长度，级总字节数:4+4+4(消息头)+6+16+1+4(消息主体)
        connect.setTotalLength(CommandId.CMPP_CONNECT.getMsgLength());
        //标识创建连接
        connect.setCommandId(CommandId.CMPP_CONNECT.getCommand());
        //序列，由我们指定
        connect.setSequenceId(Utils.getSequence());
        //我们的企业代码
        connect.setSourceAddr(cmppClientConfig.getSpId());
        //时间戳(MMDDHHMMSS)
        connect.setTimestamp(Integer.parseInt(Utils.getTimestamp()));
        byte[] authenticatorSource = Utils.getAuthenticatorSource(cmppClientConfig.getSpId(), cmppClientConfig.getSharedSecret(), connect.getTimestamp()+"");
        //md5(企业代码+密匙+时间戳)
        connect.setAuthenticatorSource(authenticatorSource);
        //版本号 高4bit为3，低4位为0
        connect.setVersion(CMPP_VERSION);
        IoSession session = (IoSession) cacheService.get(cmppClientConfig.getSpId());
        WriteFuture write = session.write(connect.toBytes());
        return write.isDone()+"";

    }

}
